<template>
  <div class="app-container search-list">
    <table-pagination
        ref="el-search-table"
        :fetch="getData"
        type="remote"
        :page-sizes="[20, 50, 100]"
        :columns="columns"
        :form-options="formOptions"
        :adaptive = "fixOption"
        height="100px"
        list-field="records"
        total-field="totalElements"
        page-index-key="page"
        page-size-key="pageSize"
        method="get">
      <template slot="button">
        <el-button size="small" type="primary" icon="el-icon-circle-plus" @click="handleAdd">新增私有应用</el-button>
      </template>
      <el-table-column label="操作" slot="append">
        <template slot-scope="scope">
          <el-button  size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </table-pagination>
  </div>
</template>

<script>
import TablePagination from '_c/Tables/table-pagination';
import { page, delAppPrivate } from '@/api/app-market';
export default {
    name: 'app-private-list',
    components: {
        TablePagination
    },
    data () {
        return {
            fixOption: {fixedHeader: true},
            list: null,
            listLoading: true,
            loadingBtn: false,
            pageParams: {
                page: 1,
                pageSize: 20,
                name: ''
            },
            formOptions: {
                showResetBtn: false,
                inline: false,
                labelWidth: 120,
                submitBtnText: '查询',
                size: 'small',
                resetBtnCallback: () => {
                    this.$refs['el-search-table'].fetchHandler(this.pageParams, false);
                },
                forms: [
                ]
            },
            columns: [
                {label: '应用名', prop: 'appName'},
                {label: 'token', prop: 'token'},
                {label: '开发者邮箱', prop: 'developerEmail'}
            ],
            tableData: []
        };
    },
    methods: {
        getData (params) {
            params.siteId = this.siteId;
            return new Promise((resolve, reject) => {
                page({...this.pageParams, ...params}).then((res) => {
                    if (res.code === '200') {
                        resolve(res.data);
                    }
                });
            });
        },
        handleAdd () {
            this.$router.push({name: 'app-private-add'});
        },
        handleDelete (id) {
            this.$confirm('此操作将永久删除该应用, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                console.log(id + ' 2 ' + this.siteId);
                delAppPrivate(id, this.siteId).then(res => {
                    if (res.code === '200') {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.$refs['el-search-table'].searchHandler(true);
                    } else {
                        this.$message({type: 'error', message: '删除出错'});
                    }
                });
            }).catch(() => {
                console.log(id + ' 1 ' + this.siteId);
            });
        }
    },
    computed: {
        siteId () {
            return this.$store.state.user.siteId;
        }
    }

};
</script>
